package me.monster.smarttraffic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;

import androidx.annotation.Nullable;

import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import me.monster.smarttraffic.base.BaseActivity;
import me.monster.smarttraffic.bo.DataHolder;
import me.monster.smarttraffic.tool.KvUtil;
import me.monster.smarttraffic.ui.dash.DashActivity;
import me.monster.smarttraffic.ui.login.LoginActivity;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-10 16:22
 */
public class SplashActivity extends BaseActivity {
    public static final String SPK_TOKEN = "SPK_TOKEN";
    public static final String SPK_LAUNCH_COUNT = "SPK_LAUNCH_COUNT";
    public static final String SPK_ACCOUNT = "SPK_ACCOUNT";
    public static final String SPK_AVATAR = "SPK_AVATAR";
    public static final String SPK_QUOTE = "SPK_QUOTE";

    public static void start(Context context) {
        Intent starter = new Intent(context, SplashActivity.class);
        context.startActivity(starter);
    }

    private DataHolder instance;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = DataHolder.getInstance();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onStart() {
        super.onStart();
        prepareCacheFolder();
        KvUtil kvUtil = KvUtil.getInstance();
        instance.setLaunchCount(kvUtil.getInt(SPK_LAUNCH_COUNT, 0));
        instance.setToken(kvUtil.getString(SPK_TOKEN, ""));
        instance.setAccount(kvUtil.getString(SPK_ACCOUNT, ""));
        instance.setAvatar(kvUtil.getString(SPK_AVATAR, ""));
        instance.setQuote(kvUtil.getString(SPK_QUOTE, ""));

        kvUtil.editor()
                .put(SPK_LAUNCH_COUNT, instance.getLaunchCount() + 1)
                .apply();
    }

    private void prepareCacheFolder() {
        File externalFilesDir = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
        if (externalFilesDir == null) {
            return;
        }
        if (externalFilesDir.exists()) {
            int length = 0;
            File[] files = externalFilesDir.listFiles();
            if (files != null) {
                length = files.length;
                instance.setVideoFileArray(files);
            }
            Logger.d("文件夹路径：" + externalFilesDir.getPath() + " 下文件有 " + length + " 个");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Timer()
                .schedule(new TimerTask() {
                    @Override
                    public void run() {
                        gotoNext();
                    }
                }, (BuildConfig.DEBUG ? 1 : 3) * 1000);
    }

    private void gotoNext() {
        if (instance.isLogin()) {
            DashActivity.start(this);
        } else {
            // TODO: 2020-02-11 根据 launchCount 是否显示引导页
            LoginActivity.start(this);
        }
        this.finish();
    }
}
